import tensorflow as tf
from tensorflow import keras
from sklearn.datasets import fetch_california_housing
from sklearn.preprocessing import StandardScaler
from sklearn.model_selection import train_test_split
from self_mudle.plot_learning_curves import plot_learning_curves

dataset = fetch_california_housing()
train_x, test_x, train_y, test_y = train_test_split(dataset.data,dataset.target,test_size=0.2,random_state=42)
train_x, valid_x, train_y, valid_y = train_test_split(train_x,train_y,test_size=0.2,random_state=42)


scaler = StandardScaler()
train_x = scaler.fit_transform(train_x)
valid_x = scaler.transform(valid_x)
test_x = scaler.transform(test_x)

# 多的一层输出直接从hidden2输出
input_wide = keras.layers.Input(shape=(5))
input_deep = keras.layers.Input(shape=(6))
hidden1 = keras.layers.Dense(30, activation='relu')(input_deep)
hidden2 = keras.layers.Dense(30, activation='relu')(hidden1)
concat = keras.layers.concatenate([input_wide, hidden2])
output = keras.layers.Dense(1)(concat)
output2 = keras.layers.Dense(1)(hidden2)

model = keras.models.Model(inputs=[input_wide, input_deep], outputs=[output,output2])

# 修改输入的数据
# 修改fit evaluate中的格式 
# 输出要给两份
train_x_wide = train_x[:,:5]
train_x_deep = train_x[:,2:]
valid_x_wide = valid_x[:,:5]
valid_x_deep = valid_x[:,2:]
test_x_wide = test_x[:,:5]
test_x_deep = test_x[:,2:]

model.summary()

model.compile( 
    optimizer = keras.optimizers.Adam(0.001),
    loss = keras.losses.mean_squared_error
)

history = model.fit([train_x_wide,train_x_deep], [train_y, train_y], epochs=10, 
                    validation_data = ([valid_x_wide,valid_x_deep], [valid_y,valid_y]))
model.evaluate([test_x_wide, test_x_deep],[test_y,test_y])
# plot_learning_curves(history)
